package com.aistar.msg;

import com.aistar.mapper.DeliveryMapper;
import com.aistar.model.bo.WebSocketProcess;
import com.aistar.model.entity.Delivery;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Employer;
import com.aistar.service.common.RedisLock;
import com.aistar.service.job.IDeliveryService;
import com.aistar.util.DateUtil;
import com.aistar.util.RedisLockTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: MsgReceiver
 * @author: LiuWenXiu
 * @date: 2021/10/28 18:44
 * @Description: TODO...本类用来演示:
 */
@Component
public class MsgReceiver {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IDeliveryService deliveryService;

    @Autowired
    private WebSocketProcess webSocketProcess;

//    @Autowired
//    private RedisLock redisLock;
//
    @Autowired
    private RedisLockTool redisLockTool;

    /**
     * 接收消息方法
     */
    public void resumeDelivery(String key) {

        // 根据key从数据库中读取数据,然后往表里插数据
        HashOperations hop = redisTemplate.opsForHash();

        // 首先加锁
//        Boolean flag = redisLock.tryLock("key_"+key,1, TimeUnit.MINUTES);
        String uuid = UUID.randomUUID().toString();
        Boolean flag = redisLockTool.lock("key_"+key,uuid,60);
//        System.out.println("正在执行....flag="+flag);
        if(flag){
            try{
                // 获取redis中的数据
                // 获取第一种数据->
                String key1 = "dev1_" + key;
                if(redisTemplate.hasKey(key1)){
                    Integer resumeId = Integer.valueOf((String) hop.get(key1,"resumeId"));
                    Integer recruitmentId = Integer.valueOf((String) hop.get(key1,"recruitmentId"));
                    String dt = (String)hop.get(key1,"deliveryTime");
                    Date deliveryTime = DateUtil.parse(dt,"yyyy-MM-dd HH:mm:ss");
                    // 执行删除操作
                    redisTemplate.delete(key1);
                    // 执行真正的投递->往表中插数据

                    deliveryService.saveToMysql(resumeId,recruitmentId,deliveryTime);
                }
            }finally {
                // 释放锁
//                redisLock.releaseLock("key_"+key);
                redisLockTool.releaseLock("key_"+key,uuid);
            }

        }


        // 获取第二种数据->
        String key2 = "dev2_" + key;
        Integer employerId = Integer.valueOf((String) hop.get(key2,"employerId"));
        String employeeName = (String) hop.get(key2,"employeeName");


        // 处理websocket
        // 发送信息到招聘者
        String msg = employeeName + "向您投递了简历,请到未处理简历中查看!";

        try {
            webSocketProcess.sendMessage(employerId,msg);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
